package com.dc.controller;

import com.dc.em.ErrorMessageEnum;
import com.dc.model.Category;
import com.dc.model.User;
import com.dc.service.CategoryService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@Api("分类模块")
@RequestMapping("/Category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @RequestMapping(value = "/selectPageCategory",method = RequestMethod.GET)
    @ApiOperation(value = "分页查询商品信息",httpMethod = "GET",notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    public BaseModel selectPageCategory (Integer curr_page, Integer page_size, BaseModel baseModel, HttpServletRequest request) throws Exception{
        return categoryService.selectPageCategory(curr_page, page_size, baseModel);
    }
    @RequestMapping(value = "/addCategory",method = RequestMethod.POST)
    @ApiOperation(value = "新增商品",httpMethod = "POST",notes = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类名称",name = "category_name",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "父级id",name = "parent_id",required = false,dataType = "int",paramType = "query")
    })
    @ResponseBody
    public BaseModel addShoes(Category category, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        category.setCreate_user_id(user.getUser_id());
        category.setIs_deleted(0);
        category.setVersion(0);
        return categoryService.addCategory(category,baseModel);
    }
    @RequestMapping(value = "/deletedCategory/{ids}",method = RequestMethod.POST)
    @ApiOperation(value="删除商品",httpMethod = "POST",notes = "删除")
    @ResponseBody
    public BaseModel deletedCategory(@PathVariable("ids") String ids, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        return categoryService.deletedCategory(ids,baseModel);
    }
    @RequestMapping(value = "/selectCategoryById/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "根据id查询商品",httpMethod = "POST",notes = "查询")
    @ResponseBody
    public BaseModel selectCategoryById(@PathVariable("id") Integer id,BaseModel baseModel,HttpServletRequest request) throws Exception{
        return categoryService.selectCategory(id,baseModel);
    }
    @RequestMapping(value = "/updateCategory",method = RequestMethod.POST)
    @ApiOperation(value = "修改商品",httpMethod = "POST",notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类ID",name = "category_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "分类名称",name = "category_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "父级",name = "parent_id",required = false,dataType = "Integer",paramType = "query"),
    })
    @ResponseBody
    public BaseModel updateCategory(Category category, BaseModel baseModel, HttpServletRequest request) throws Exception{
        return categoryService.updateCategory(category,baseModel);
    }
}
